package com.jaxb.binder;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

/**
 * A simple implementation of BtStoneBinder.
 * Just a demo, do not use this implementation.
 * For faster operation, Use cached binder instead.
 * 
 * @author yyjiangc
 * @version 1.0 03/07/2011
 */
public class SimpleStoneBinder extends AbstractStoneBinder implements BtStoneBinder {

	/**
	 * Create a Unmarshaller object.
	 * 
	 * @param objFactoryClass the class object 
	 * @return the Unmarshaller object created
	 * @throws JAXBException throws Exception if error occurs when create Unmarshaller. 
	 */
	@Override
	public Unmarshaller createUnmarshaller(Class<?> objFactoryClass) 
	    throws JAXBException {
		JAXBContext jaxbContext = JAXBContext.newInstance(objFactoryClass);
		return jaxbContext.createUnmarshaller();
	}
	
	/**
	 * Create a Marshaller object.
	 * 
	 * @param objFactoryClass the class object
	 * @return the Marshaller object created
	 * @throws JAXBException throws Exception if error occurs when create Marshaller.
	 */
	@Override
	public Marshaller createMarshaller(Class<?> objFactoryClass) 
        throws JAXBException {
		JAXBContext jaxbContext = JAXBContext.newInstance(objFactoryClass);
		Marshaller marshaller = jaxbContext.createMarshaller();
		marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
		return marshaller;
	}
	
}
